package cn.imvc.app.models;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.controllers.goods.GoodsPriceControllerImpl;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsPrice;
import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.entities.PriceClass;
import cn.imvc.app.repositories.GoodsPriceRepository;
import cn.imvc.app.repositories.GoodsRepository;
import cn.imvc.app.repositories.GoodsSkuRepository;
import cn.imvc.app.repositories.PriceClassRepository;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsPriceModelImpl implements GoodsPriceModel {
	static Logger logger = Logger.getLogger(GoodsPriceModelImpl.class);
	@Autowired
	GoodsPriceRepository goodsPriceRepository;
	@Autowired 
	PriceClassRepository priceClassRepository;
	@Autowired
	GoodsRepository goodsRepository;
	@Autowired
	GoodsSkuRepository goodsSkuRepository;
	@Autowired
	GoodsReviewsModel goodsReviewsModel;
	@Autowired
	LoggingModel loggingModel;
	
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<GoodsPrice> find(ViewRequest viewRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	public GoodsPrice get(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public GoodsPrice save(GoodsPrice goodsPrice) {
//		Goods goods = goodsRepository.getOne(goodsImage.getGoods().getId());
//		goodsImage.setGoods(goods);
		GoodsPrice goodsPrice2 = goodsPriceRepository.findOne(goodsPrice.getId());
		goodsPrice2.setPrice(goodsPrice.getPrice());
		return goodsPriceRepository.saveAndFlush(goodsPrice2);
	}

	public GoodsPrice update(GoodsPrice goodsPrice) {
//		Goods goods = goodsRepository.getOne(goodsImage.getGoods().getId());
//		goodsImage.setGoods(goods);
		GoodsPrice goodsPrice2 = goodsPriceRepository.findOne(goodsPrice.getId());
		goodsPrice2.setPrice(goodsPrice.getPrice());
		return goodsPriceRepository.saveAndFlush(goodsPrice2);
	}

	public void delete(int id) {
		// TODO Auto-generated method stub

	}

	public void deleteBatch(int[] ids) {
		// TODO Auto-generated method stub

	}

	/**
	 * 当不存在价格时，向价格表中插入空的价格
	 */
	public void updateEmptyPrice(GoodsSku goodsSku) {
		List<PriceClass> priceClasses = priceClassRepository.findAll();
		Iterator<PriceClass> iterator = priceClasses.iterator();
		Goods goods = goodsRepository.findOne(goodsSku.getGoods().getId());
		GoodsSku goodsSkuTmp = goodsSkuRepository.findOne(goodsSku.getId());
		while (iterator.hasNext()) {
			PriceClass priceClass = (PriceClass) iterator.next();
			GoodsPrice goodsPriceTmp = new GoodsPrice();
			goodsPriceTmp.setGoods(goodsSku.getGoods());
			goodsPriceTmp.setGoodsSku(goodsSku);
			goodsPriceTmp.setPriceClass(priceClass);
			List<GoodsPrice> goodsPriceSearchs = goodsPriceRepository.findAllJpql("select goodsPrice from GoodsPrice goodsPrice where goodsPrice.goods.id = '"+goods.getId()
				+"' and goodsPrice.goodsSku.id = '"+goodsSku.getId()+"' and goodsPrice.priceClass.id = '"+priceClass.getId()+"'");
			if(goodsPriceSearchs.size() < 1){
				goodsPriceTmp.setGoods(goods);
				goodsPriceTmp.setGoodsSku(goodsSkuTmp);
				goodsPriceTmp.setPriceClassName(priceClass.getName());
				goodsPriceRepository.saveAndFlush(goodsPriceTmp);
			}
		}
	}

	public List<GoodsPrice> findCurSkuPrice(GoodsSku goodsSku){
		List<GoodsPrice> goodsPriceSearchs = goodsPriceRepository.findAllJpql("select goodsPrice from GoodsPrice goodsPrice where goodsPrice.goods.id = '"
					+goodsSku.getGoods().getId()+"' and goodsPrice.goodsSku.id = '"+goodsSku.getId()+"'");
		return goodsPriceSearchs;
	}

	public void deleteByGoods(Goods goods) {
		goodsPriceRepository.deleteByGoods(goods);
	}

	public void deleteByGoodsSku(GoodsSku goodsSku) {
		goodsPriceRepository.deleteByGoodsSku(goodsSku);
	}

	public void updatePriceBatch(List<GoodsPrice> goodsPrices,List<Double> prices,int goodsId) {
		Iterator<GoodsPrice> iterator = goodsPrices.iterator();
		GoodsSku goodsSku = null;
		int id = goodsId;
		for (int i = 0; i < goodsPrices.size(); i++) {
			GoodsPrice goodsPrice = goodsPrices.get(i);
			double price = prices.get(i);
			goodsPrice.setPrice((int)Math.round(price*100));
			update(goodsPrice);
			if(goodsSku == null){
				goodsSku = goodsPrice.getGoodsSku();
			}
			if(!goodsPrices.isEmpty()){
				goodsSku = goodsPriceRepository.getOne(goodsPrices.get(0).getId()).getGoodsSku();
				loggingModel.info(logger, "更新sku："+goodsSku.getSubTitle()+"的"+goodsPrice.getPriceClassName()+"价格为:"+price);
			}
		}
		if(!goodsPrices.isEmpty()){
			goodsSku = goodsPriceRepository.getOne(goodsPrices.get(0).getId()).getGoodsSku();
		}
		updateGoodsLowPrice(id);
		Goods goods = goodsRepository.findOne(id);
		if(goods.getState() == 1 || goods.getState() == 3){
			Goods goodsTmp = null;
			goods.setVerify((byte)10);
			goods.setState((byte)(goods.getState()+10));
			goodsTmp = goodsRepository.saveAndFlush(goods);
			goodsReviewsModel.saveReviews(goodsTmp);
			//同步时过滤已下架状态
			if(goodsSku.getState() != 0 || goodsTmp.getState() == 0){
				goodsSku.setState((byte)goodsTmp.getState());
				goodsSkuRepository.saveAndFlush(goodsSku);
			}
		}
//		if(goods.getState() == 1){
//			//设置为重新审核
//			Goods goodsTmp = null;
//			goods.setVerify((byte)10);
//			goods.setState((byte)0);
//			goodsTmp = goodsRepository.saveAndFlush(goods);
//			goodsReviewsModel.saveReviews(goodsTmp);
//		}
	}

	public void updateGoodsLowPrice(int id) {
		List<Integer> prices = goodsPriceRepository.getLowPriceByClass(id);
		List<Integer> prices2 = goodsPriceRepository.getLowMemberPrice(id);
		Integer goodsPoints = goodsSkuRepository.getLowPoints(id);
		Goods goods = goodsRepository.findOne(id);
		int goodsPrice=0,goodsPrice2=0;
		if(!prices.isEmpty()){
			goodsPrice = prices.get(0) == null ? 0 : prices.get(0);
			goodsPrice2 = prices2.get(0) == null ? 0 : prices2.get(0);
		}
		goods.setPoints(goodsPoints);
		goods.setPrice(goodsPrice);
		goods.setPrice2(goodsPrice2);
		goodsRepository.saveAndFlush(goods);
	}
	
	public List<GoodsPrice> getGoodsPrices(int goodsId){
		return goodsPriceRepository.getGoodsPrices(goodsId);
	}
}
